//动态路由配置
import router from './index';
import {
    routes,
    roleRouter,
    roleRoute
} from "./index";
import {
    getToken,
    getUserId,
    getRole
} from "@/utils/auth";
import store from '@/store'
import Vue from 'vue';


// const roleLevel = 1;

//设置左侧菜单路由 根据对应的用户权限生成
export const leftMenuRouter = (list) => {
    let roleLevel = getRole();
    //超级管理员 添加全部路由
    let _newUserRouter = {};
    if(roleLevel == 1){
        //业务人员
        _newUserRouter = roleRoute.businessUserRoute;
    }else if(roleLevel == 0){
        //子公司
        _newUserRouter = roleRoute.childCompanyRoute;
    }else{
        _newUserRouter = {}
    }
    // _newUserRouter = roleRouter;
    return [
        ...routes,
        ...Object.values(_newUserRouter)
    ];
    return;
    let userRouter = store.state.user.roleRouter;
    // console.log("左侧userRouter",userRouter)
    // let _newUserRouter = {};
    let userId = getUserId();
    if (userId == Vue.prototype.$superId) {
        //超级管理员 添加全部路由
        _newUserRouter = roleRouter;
    } else {
        //普通用户 再判断权限生成对应的路由
        Object.values(roleRouter).forEach(route => {
            let name = route.name;
            if (userRouter[name] && userRouter[name].status == 1) {
                //显示父级 判断子级
                //获取要显示的子级name数组
                let roleChildName = userRouter[name].children.filter(item => item.status == 1).map(item => item.name);
                //获取原始子级路由数据
                let originChild = route.children;
                let newParent = {
                    ...route,
                    children: []
                }
                //循环判断要显示的子级并追加
                originChild.forEach(t => {
                    let childName = t.name;
                    if (roleChildName.includes(childName)) {
                        newParent.children.push(t);
                    }
                });
                _newUserRouter[name] = newParent;
                // router.addRoute(newParent);
            }
        });
    }
    // console.log("侧边目录权限路由",_newUserRouter)
    return [
        ...routes,
        ...Object.values(_newUserRouter)
    ];
}

//设置当前用户的权限信息
export const setRoleRoute = async (list) => {
    let roleLevel = getRole();
    if(roleLevel == 1){
        //业务人员
        Object.values(roleRoute.businessUserRoute).forEach(route => {
            router.addRoute(route);
        });
    }
    else if(roleLevel == 0){
        //子公司
        Object.values(roleRoute.childCompanyRoute).forEach(route => {
            router.addRoute(route);
        });
    }

    return;
    if (!getToken()) return;
    //先获取用户权限路由并存储
    await store.dispatch("user/setRoleRouter");
    let userRouter = store.state.user.roleRouter;
    if (!userRouter) return;
    let userId = getUserId();
    if (userId == Vue.prototype.$superId) {
        //超级管理员 添加全部路由
        console.log('设置当前用户路由-我他妈是超级管理员啦');
        Object.values(roleRouter).forEach(route => {
            router.addRoute(route);
        });
    } else {
        //普通用户 先删除设置-用户设置路由 再判断权限生成对应的路由
        console.log('设置当前用户路由-我他妈是普通用户啦');
        Object.values(roleRouter).forEach(route => {
            let name = route.name;
            if (userRouter[name] && userRouter[name].status == 1) {
                //显示父级 判断子级
                //获取要显示的子级name数组
                let roleChildName = userRouter[name].children.filter(item => item.status == 1).map(item => item.name);
                //获取原始子级路由数据
                let originChild = route.children;
                let newParent = {
                    ...route,
                    children: []
                }
                //循环判断要显示的子级并追加
                originChild.forEach(t => {
                    let childName = t.name;
                    if (roleChildName.includes(childName)) {
                        newParent.children.push(t);
                    }
                });
                router.addRoute(newParent);
            } else {
                //隐藏 不做添加路由操作
            }
        });
    }

}

//重置所有权限路由 用于用户退出登录时使用 vue3.0版本不支持removeRoute api 需要升级更高版本
export const resetRouter = () => {
    console.log("清楚所有权限路由")
    Object.values(roleRouter).forEach(route => {
        const {
            name
        } = route;
        if (name) {

            // router.hasRoute(name) && router.removeRoute(name);
        }
    });
}

export let roleTemplateRouter = {
    "shipperManager": {
        "name": "shipperManager",
        "title": "货主管理",
        "status": 1,
        "children": [{
            "name": "shipperManagerIndex",
            "title": "货主审核",
            "status": 1
        }]
    },
    "driverCarManager": {
        "name": "driverCarManager",
        "title": "司机车辆管理",
        "status": 1,
        "children": [{
                "name": "driverAuditing",
                "title": "司机审核",
                "status": 0
            },
            {
                "name": "carAuditing",
                "title": "车辆审核",
                "status": 1
            },
            {
                "name": "driverList",
                "title": "司机列表",
                "status": 0
            }
        ]
    },
    "waybillManagement": {
        "name": "waybillManagement",
        "title": "运单管理",
        "status": 1,
        "children": [{
                "name": "allList",
                "title": "总运单列表",
                "status": 1
            },
            {
                "name": "inList",
                "title": "进行中运单",
                "status": 0
            },
            {
                "name": "suspendList",
                "title": "已暂停运单",
                "status": 1
            },
            {
                "name": "trackList",
                "title": "分运单追踪",
                "status": 1
            }
        ]
    },
    "moneyManagment": {
        "name": "moneyManagment",
        "title": "财务管理",
        "status": 1,
        "children": [{
                "name": "auditing",
                "title": "财务审核",
                "status": 1
            },
            {
                "name": "withdrawalRecord",
                "title": "提现记录",
                "status": 1
            },
            {
                "name": "collect",
                "title": "汇总代开",
                "status": 1
            },
            {
                "name": "shipperStream",
                "title": "货主流水",
                "status": 1
            },
            {
                "name": "driverStream",
                "title": "司机流水",
                "status": 1
            },
            {
                "name": "vaildate",
                "title": "支付结果验证",
                "status": 1
            },
            {
                "name": "invoiceManage",
                "title": "发票管理",
                "status": 1
            },
            {
                "name": "invoiceDetail",
                "title": "发票明细",
                "status": 1
            },
            {
                "name": "bankCardManagment",
                "title": "常用银行卡管理",
                "status": 1
            },
            {
                "name": "prePay",
                "title": "预支付列表",
                "status": 1
            }
        ]
    },
    "GoodsManagment": {
        "name": "GoodsManagment",
        "title": "货源管理",
        "status": 1,
        "children": [{
            "name": "list",
            "title": "货源列表",
            "status": 1
        }]
    },
    "PageConfig": {
        "name": "PageConfig",
        "title": "官网配置",
        "status": 1,
        "children": [{
                "name": "list",
                "title": "配置列表",
                "status": 1
            },
            {
                "name": "edit",
                "title": "配置数据",
                "status": 1
            }
        ]
    },
    "VIP": {
        "name": "VIP",
        "title": "会员管理",
        "status": 1,
        "children": [{
            "name": "list",
            "title": "会员审核",
            "status": 1
        }]
    },
    "set": {
        "name": "set",
        "title": "设置",
        "status": 1,
        "children": [{
                "name": "setindex",
                "title": "总配置",
                "status": 1
            },
            {
                "name": "info",
                "title": "平台信息",
                "status": 1
            },
            {
                "name": "user_Version",
                "title": "用户日志",
                "status": 1
            },
            {
                "name": "log",
                "title": "平台日志",
                "status": 1
            },
            {
                "name": "statistics",
                "title": "资源统计",
                "status": 1
            },
            {
                "name": "userManage",
                "title": "用户设置",
                "status": 1
            }
        ]
    }
}
// export default totalRouter;